package nickthissen.iracing.activities;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Toast;

import nickthissen.iracing.common.DataLoader;
import nickthissen.iracing.common.Error;
import nickthissen.iracing.common.Task;
import nickthissen.iracing.datamodels.Result;
import nickthissen.iracing.iracingforum2.R;
import nickthissen.iracing.networking.Networking;
import nickthissen.iracing.parsing.HttpUtil;
import nickthissen.iracing.parsing.MessageParser;

public class PostThreadActivity extends PostBaseActivity
{
    private static final String FORUM_ID = "FORUM_ID";
    private static final String URL = "URL";

    private String storedUrl;
	private int forumId;

	private ProgressDialog dialog;
		
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        txtTitle.setVisibility(View.VISIBLE);
        txtUser.setVisibility(View.GONE);
        btnFindUser.setVisibility(View.GONE);

        txtTitle.requestFocus();

        Bundle extras = savedInstanceState;
        if (extras == null)
        {
            Intent i = this.getIntent();
            extras = i.getExtras();
        }
        forumId = extras.getInt(FORUM_ID);

        this.getSupportActionBar().setTitle("Create new thread");
    }
	
	@Override
	public void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
        outState.putInt(FORUM_ID, forumId);
	}

    @Override
    public void onRestoreInstanceState(Bundle state)
    {
        forumId = state.getInt(FORUM_ID);
        super.onRestoreInstanceState(state);
    }

    @Override
    protected Task<?> createTask()
    {
        return null;
    }

    @Override
    protected void saveTaskInput(Bundle state)
    {
        state.putString(URL, storedUrl);
    }

    @Override
    protected Object restoreTaskInput(Bundle state)
    {
        return state.getString(URL);
    }

	private static Bundle getIntentExtras(int forumId)
	{
		Bundle extras = new Bundle();
		extras.putInt(FORUM_ID, forumId);
		return extras;
	}
	
	public static Intent getPostIntent(Context context, int forumId)
	{
		Intent intent = new Intent(context, PostThreadActivity.class);
		Bundle extras = getIntentExtras(forumId);
		intent.putExtras(extras);
		return intent;
	}

	@Override
	protected void onPostButtonClicked()
	{
		dialog = ProgressDialog.show(this, "Posting", "Your thread is being created...");
		new Task<Void>(this, poster).execute();
	}

    private DataLoader<Void> poster = new DataLoader<Void>()
    {
        @Override
        public Result getData(Void in)
        {
            String title = txtTitle.getText().toString();
            String text = txtContent.getText().toString();

            title = HttpUtil.htmlEncode(title);
            text = HttpUtil.htmlEncode(text);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
            boolean attachSig = prefs.getBoolean(this.getContext().getString(R.string.PREF_AttachSignature), true);
            return Networking.postThread(this.getContext(), forumId, title, text, attachSig);
        }

        @Override
        public void onFinished(Result out)
        {
            if (out.Success)
            {
                Toast.makeText(this.getContext(),
                        "Thread posted!",
                        Toast.LENGTH_LONG).show();
            }
            else
            {
                Error.fromResult(out).log(this.getContext());

                @SuppressWarnings("deprecation")
                ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboard.setText(txtContent.getText().toString());

                Toast.makeText(this.getContext(),
                        "Error creating thread, please try again. Your message has been saved in your clipboard.",
                        Toast.LENGTH_LONG).show();
            }

            postFinished();

            if (dialog != null)
            {
                dialog.dismiss();
                dialog = null;
            }
        }
    };
}
